package top.saybook.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import top.saybook.CacheKey;
import top.saybook.crawlerutil.CrawlerUtil;
import top.saybook.entity.WebUrl;
import top.saybook.mapper.BookMapper;
import top.saybook.mapper.ChapterMapper;
import top.saybook.pojo.Book;
import top.saybook.pojo.Chapter;
import top.saybook.service.BookService;

import java.util.*;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    // 查询所有
    public List<Book> selectAll(String bookName) {
        List<Book> redisBookList = redisTemplate.boundHashOps(CacheKey.BOOK).values();
        if(redisBookList.size()==0){
            Example example = new Example(Book.class);
            Example.Criteria criteria = example.createCriteria();
            if(bookName!=null && !"".equals(bookName)){
                criteria.andEqualTo("bookName",bookName);
            }
            List<Book> dbBookList = bookMapper.selectByExample(example);
            if(dbBookList.size()!=0 && dbBookList!=null){
                for (Book book : dbBookList) {
                    redisTemplate.boundHashOps(CacheKey.BOOK).put(book.getId(),book);
                }
                return dbBookList;
            }
            List<Map> crawlerBookList = CrawlerUtil.crawlerBook(WebUrl.BIQUGE_URL, WebUrl.BIQUGEBOOKLIST);
            List<Book> bookList = mapListToBookList(crawlerBookList);
            bookMapper.insertList(bookList);
            if(bookList.size()!=0 && bookList!=null){
                for (Book book : bookList) {
                    redisTemplate.boundHashOps(CacheKey.BOOK).put(book.getId(),book);
                }
            }
            return bookList;
        }
        if(bookName!=null && !"".equals(bookName)){
            return getRedisByName(redisBookList,bookName);
        }
        return redisBookList;
    }


    public Map findChapterList(Integer bookId) {
        Map map = new HashMap<>();
        List<Chapter> redisChapterList = (List<Chapter>) redisTemplate.boundHashOps(CacheKey.CHAPTER).get(bookId);
        map.put("chapterList",redisChapterList);
        if(redisChapterList == null || redisChapterList.size()==0){
            Example example = new Example(Chapter.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("bookId",bookId);
            List<Chapter> dbChapterList = chapterMapper.selectByExample(example);
            if(dbChapterList!=null && dbChapterList.size()!=0){
                redisTemplate.boundHashOps(CacheKey.CHAPTER).put(bookId,dbChapterList);
                map.put("chapterList",dbChapterList);
            }else {
                Book book = bookMapper.selectByPrimaryKey(bookId);
                List<Map> mapChapterList = CrawlerUtil.crawlerBookChater(book.getLinkUrl(), WebUrl.BIQUGEBOOKCHAPTERLIST);
                List<Chapter> chapterList = mapChapterListToChapterList(mapChapterList, book.getId());
                chapterMapper.insertList(chapterList);
                redisTemplate.boundHashOps(CacheKey.CHAPTER).put(bookId, chapterList);
                map.put("chapterList",chapterList);
            }

        }
        Book book = (Book) redisTemplate.boundHashOps(CacheKey.BOOK).get(bookId);

        map.put("book",book);
        return map;
    }

    @Override
    public Map findByChapterContent(Integer chapterId) {
        Map map = new HashMap<>();
        Chapter chapter = chapterMapper.selectByPrimaryKey(chapterId);
        String content = (String) redisTemplate.boundHashOps(chapter.getBookId()).get(chapterId);
        if(content==null){
            content = CrawlerUtil.crawlerChapterContent(chapter.getLinkUrl(),WebUrl.BIQUGEBOOKCHAPTERCONTENT);
            if(content!=null && !"".equals(content)){
                redisTemplate.boundHashOps(chapter.getBookId()).put(chapterId,content);
            }
        }
//        Book book = bookMapper.selectByPrimaryKey(chapter.getBookId());
        Book book = (Book) redisTemplate.boundHashOps(CacheKey.BOOK).get(chapter.getBookId());
        map.put("content",content);
        map.put("chapterName",chapter.getChapter());
        map.put("book",book);
        return map;
    }


    public List<Book> getRedisByName(List<Book> bookList ,String bookName){
        List<Book> books = new ArrayList<>();
        for (Book book : bookList) {
            if(book.getBookName().indexOf(bookName)!=-1){
                books.add(book);
            }
        }
        return books;
    }

    /**
     * 将爬取到的书籍map列表转换为书籍book列表
     * @param mapList 爬取到的mapList
     * @return
     */
    public List<Book> mapListToBookList(List<Map> mapList){
        List<Book> bookList = new ArrayList<>();
        for (Map map : mapList) {
            Book book = new Book();
            book.setBookName((String) map.get("bookName"));
            book.setLinkUrl((String) map.get("linkUrl"));
            book.setDate(new Date());
            book.setIsShow(1);
            book.setNewdate(new Date());
            book.setIsPlay(0);
            bookList.add(book);
        }
        return  bookList;
    }

    // 章节map集合转换成chapter集合
    public List<Chapter> mapChapterListToChapterList(List<Map> mapChapterList,Integer bookId) {
        List<Chapter> chapterList = new ArrayList<>();

        for (Map map : mapChapterList) {
            Chapter chapter = new Chapter();
            chapter.setBookId(bookId);
            chapter.setChapter((String) map.get("chapterName"));
            chapter.setLinkUrl((String) map.get("linkUrl"));
            chapterList.add(chapter);
        }
        return chapterList;
    }
}
